package com.ukao.pad.ui;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextClock;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;
import com.ukao.pad.R;
import com.ukao.pad.base.BaseActivity;
import com.ukao.pad.bean.SearchHistorysBean;
import com.ukao.pad.consts.SaveParamets;
import com.ukao.pad.db.SearchHistoryDao;
import com.ukao.pad.utils.AppUtils;
import com.ukao.pad.utils.CheckUtils;
import com.ukao.pad.utils.L;
import com.ukao.pad.widget.ShapedImageView;
import com.ukao.pad.widget.StateImageView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;



public class LoginActivity extends BaseActivity {


    public static final int MSG_ONE = 1;
    @BindView(R.id.wifi_name)
    TextView wifiName;
    @BindView(R.id.wifi)
    ImageView wifi;
    @BindView(R.id.left_control_menu_icon)
    ShapedImageView leftControlMenuIcon;
    @BindView(R.id.left_control_menu_username)
    TextView leftControlMenuUsername;
    @BindView(R.id.today)
    TextView today;
    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.left_control_menu_date)
    TextClock leftControlMenuDate;
    @BindView(R.id.left_control_menu_time)
    TextClock leftControlMenuTime;
    @BindView(R.id.left_control_menu_week)
    TextClock leftControlMenuWeek;
    @BindView(R.id.remove)
    StateImageView remove;
    @BindView(R.id.log_in)
    TextView logIn;
    private WifiReceiver mReceiver;
    private  String   code;
    private SearchHistoryDao dao;
    private ArrayList<SearchHistorysBean> historywordsList;
    private String strSerialNum;
    private String rfidSecretCard;
    private WifiManager mWifiManager;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (SaveParamets.getIsLogin()){
            startActivity(new Intent(this,MainActivity.class));
            finish();
            return;
        }
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        initView();
        if (!CheckUtils.isMIUIDevices()){
            Beta.checkUpgrade(false,false);/*不提示正在检查更新，2不弹提示*/
        }
    }





    @Override
    protected void initView() {
        mWifiManager= (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        setCode();
        loadRootFragment(R.id.id_layout,new SwipingCardFragment());
        wifiName.setText(AppUtils.getWIfiName(this));
        leftControlMenuIcon.setVisibility(View.INVISIBLE);
        today.setVisibility(View.INVISIBLE);
        leftControlMenuUsername.setVisibility(View.INVISIBLE);
        count.setVisibility(View.INVISIBLE);
        remove.setVisibility(View.INVISIBLE);


    }
    private void setCode() {
        code="";
        dao= new SearchHistoryDao(this);
        historywordsList= dao.findAll();

        for (SearchHistorysBean searchHistorysBean : historywordsList) {
            if (!searchHistorysBean.historyword.equals("first")){
                code=code+searchHistorysBean.historyword;
                try {

                    JSONObject mJSONObject=new JSONObject(searchHistorysBean.historyword);
                    strSerialNum=mJSONObject.getString("serialNum");

                } catch (JSONException e) {


                }
            }
        }
        Log.d("序列号", "setCode: ");

        if (!CheckUtils.isEmpty(strSerialNum)) {

            SaveParamets.setCode(this,strSerialNum);
        }
        L.d("strSerialNum="+strSerialNum);
    }


    @OnClick({R.id.log_in,R.id.wifi})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case    R.id.wifi:

                Intent  mIntent=new Intent(this,WifiActivity.class);
                startActivity(mIntent);
                break;


        }
    }






    @Override
    public void onBackPressedSupport() {

        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            pop();
        }
    }


    /**
     * 注册wifi 广播
     */
    private void registerReceiver() {
        mReceiver= new WifiReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mReceiver, filter);
    }




    private class WifiReceiver extends BroadcastReceiver {
        protected static final String TAG = "ukao";

        @Override
        public void onReceive(Context context, Intent intent) {

            if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {// wifi连接上与否
                Log.d(TAG, "网络已经改变");
                NetworkInfo info = intent
                        .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
                if (info.getState().equals(NetworkInfo.State.DISCONNECTED)) {
                    Log.d(TAG, "wifi已经断开");
                    wifi.setImageResource(R.drawable.left_control_menu_no_wifi);
                    wifiName.setText("");
                } else if (info.getState().equals(NetworkInfo.State.CONNECTING)) {
                    Log.d(TAG, "正在连接...");
                    wifi.setImageResource(R.drawable.left_control_menu_no_wifi);

                } else if (info.getState().equals(NetworkInfo.State.CONNECTED)) {
                    WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(WIFI_SERVICE);
                    WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                    Log.d(TAG, "连接到网络：" + wifiInfo.getBSSID());
                    wifi.setImageResource(R.drawable.left_control_menu_wifi);
                    String wifiId = wifiInfo != null ? wifiInfo.getSSID() : "";
                    wifiName.setText(wifiId);
                }

            }

        }
    }



    @Override
    protected void onResume() {
        super.onResume();

        wifi.setImageResource(mWifiManager.isWifiEnabled() ? R.drawable.left_control_menu_wifi : R.drawable.left_control_menu_no_wifi);
        registerReceiver();

    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);  //移除广播
    }







}
